class Student:
    def __init__(self, student_id, name, age, grade):
        self.student_id = student_id
        self.name = name
        self.age = age
        self.grade = grade


class StudentManagementSystem:
    def __init__(self):
        self.students = []

    def add_student(self):
        print("\n添加学生信息")
        student_id = input("请输入学号: ")
        # 检查学号是否已存在
        if any(s.student_id == student_id for s in self.students):
            print("该学号已存在！")
            return

        name = input("请输入姓名: ")
        age = input("请输入年龄: ")
        grade = input("请输入班级: ")

        self.students.append(Student(student_id, name, age, grade))
        print("学生信息添加成功！")

    def view_all_students(self):
        print("\n所有学生信息")
        if not self.students:
            print("暂无学生信息")
            return

        print("学号\t姓名\t年龄\t班级")
        for student in self.students:
            print(f"{student.student_id}\t{student.name}\t{student.age}\t{student.grade}")

    def search_student(self):
        print("\n查找学生")
        student_id = input("请输入要查找的学号: ")

        for student in self.students:
            if student.student_id == student_id:
                print("找到学生信息：")
                print(f"学号: {student.student_id}")
                print(f"姓名: {student.name}")
                print(f"年龄: {student.age}")
                print(f"班级: {student.grade}")
                return

        print("未找到该学生！")

    def delete_student(self):
        print("\n删除学生")
        student_id = input("请输入要删除的学号: ")

        for i, student in enumerate(self.students):
            if student.student_id == student_id:
                del self.students[i]
                print("学生信息删除成功！")
                return

        print("未找到该学生！")

    def update_student(self):
        print("\n修改学生信息")
        student_id = input("请输入要修改的学号: ")

        for student in self.students:
            if student.student_id == student_id:
                print("请输入新信息（留空则保持不变）:")
                name = input(f"新姓名 [{student.name}]: ") or student.name
                age = input(f"新年龄 [{student.age}]: ") or student.age
                grade = input(f"新班级 [{student.grade}]: ") or student.grade

                student.name = name
                student.age = age
                student.grade = grade
                print("学生信息修改成功！")
                return

        print("未找到该学生！")

    def show_menu(self):
        print("\n学生管理系统")
        print("1. 添加学生")
        print("2. 查看所有学生")
        print("3. 查找学生")
        print("4. 删除学生")
        print("5. 修改学生信息")
        print("6. 退出系统")

    def run(self):
        while True:
            self.show_menu()
            choice = input("请选择操作 (1-6): ")

            if choice == '1':
                self.add_student()
            elif choice == '2':
                self.view_all_students()
            elif choice == '3':
                self.search_student()
            elif choice == '4':
                self.delete_student()
            elif choice == '5':
                self.update_student()
            elif choice == '6':
                print("感谢使用学生管理系统！")
                break
            else:
                print("无效选择，请重新输入！")


if __name__ == "__main__":
    system = StudentManagementSystem()
    system.run()